<script>
	function AttendanceCtrl($scope, CtrlUtil, $element, modalService, UploadService, toaster, $http) {
		var ctrl = this
		this.onQuerySuccess = function(c) {
			$http.post(':root/course/getMyAttend', {
				courseId : CtrlUtil.getIds(c.content)
			}).success(function(r) {
				CtrlUtil.addProperty(c.content, r, 'attend', 'courseInfo.id')
				return r;
			})
			return $http.post(':root/course/getApplyLeaves', {
				courseId : CtrlUtil.getIds(c.content)
			}).success(function(r) {
				CtrlUtil.addProperty(c.content, r, 'applyed', 'courseInfo.id')
				return r;
			})
		}
		$http.post(':root/course/canApplyLeave').success(function(r) {
			ctrl.canApplyLeave = r
		})
		CtrlUtil.build(this, ':root/course/getMyCourses')
		this.view = function(item) {
			console.log(item)
		}
		this.apply = function(item) {
			var content = $element.find('[template]>[apply]').clone()
			var btn = content.find('[upload]')
			var result = {
				fjStatus : 0,
				courseInfo : {
					id : item.id
				}
			}
			var config = {
				multipleFiles : false,
				autoUploading : true,
				browser : btn[0],
				maxSize : 1024 * 1024 * 20,
				extFilters : [ ".jpg", ".jpeg", ".png", ".gif", ".bmp" ],
				onComplete : function(file) {
					var attach = {}
					attach.fileName = file.name;
					attach.hash = eval('(' + file.msg + ')').message;
					attach.size = file.size;
					attach.uploading = false
					result.fjId = (attach.hash + '/' + attach.size)
					result.fjStatus = 2
					$scope.$$phase || $scope.$apply()
				},
				onMaxSizeExceed : function(file) {
					toaster.pop({
						type : 'error',
						body : '文件大小不能超过20M',
						timeout : 800
					});
					result.fjStatus = 0
					$scope.$$phase || $scope.$apply()
				},
				onSelect : function(list) {
					result.fjStatus = 1
					$scope.$$phase || $scope.$apply()
				},
				onExtNameMismatch : function(file) {
					toaster.pop({
						type : 'error',
						body : '只能选择图片文件',
						timeout : 800
					});
					result.fjStatus = 0
					$scope.$$phase || $scope.$apply()
				},
			}
			var stream = UploadService.create(config)
			modalService.confirm({
				title : '请假申请 - ' + item.name,
				content : content,
				result : result,
				validator : function(r) {
					if (!r.fjId) {
						toaster.pop({
							type : 'error',
							body : '请上传附件',
							timeout : 800
						});
						$scope.$$phase || $scope.$apply()
						return false;
					}
				}
			}).then(function(r) {
				$http.post(':root/course/applyLeave', r).success(function(r) {
					modalService.succ('申请发送成功', 1500)
					ctrl.canApplyLeave = false
					$http.post(':root/course/canApplyLeave').success(function(r) {
						ctrl.canApplyLeave = r
					})
					item.applyed = r
				})
			})

		}
	}
</script>
<div ng-controller="AttendanceCtrl as ctrl" ng-init="ctrl.query()">
	<h3 class="header container-fluid media">考勤|请假</h3>
	<div class="container-fluid">
		<div class="well h4 ">
			<label>才气就是长期的坚持不懈！</label>
		</div>
	</div>

	<div class="container-fluid media">
		<table class="table table-striped table-hover table-bordered">
			<tr>
				<th>课程名称</th>
				<th>课程类型</th>
				<th>开课时间</th>
				<th>课程状态</th>
				<th>我的课程考勤状态</th>
				<th>操作</th>
			</tr>
			<tr ng-repeat="item in ctrl.entities.content">
				<td>{{item.name}}</td>
				<td>{{item.kclb|courseType}}</td>
				<td>{{item.kcsj|date:'yyyy-MM-dd HH:mm EEE'}}</td>
				<td>{{(item.status == 1 || $server.time >= item.kcsj)?'已授课':'即将开课'}}</td>
				<td>
					<span ng-if="item.applyed&&!item.attend" ng-switch="item.applyed.status">
						<span ng-switch-when="1">已请假,等待院系管理员审核</span>
						<span ng-switch-when="2">已请假,等待学校管理员审核</span>
						<span ng-switch-when="3">请假申请已审核通过</span>
						<span ng-switch-default>请假申请已被驳回</span>
					</span>
					<span ng-if="item.attend" ng-switch="item.attend.status">
						<span ng-switch-when="1">请假</span>
						<span ng-switch-when="2">缺勤</span>
						<span ng-switch-when="0">正常</span>
						<span ng-switch-default>未知</span>
					</span>
				</td>
				<td>
					<a ng-if="(item.kcsj|unexpired)&&!item.applyed&&item.kclb==1&&ctrl.canApplyLeave" href="javascript:;" ng-click="ctrl.apply(item)">我要请假</a>

				</td>
			</tr>
		</table>
		<div class="panel-body">
			共有记录：
			<span>{{ctrl.entities.totalElements}}</span>
			条
			<div class="pull-right" ng-include="':root/components/page.html'"></div>
		</div>
	</div>
	<div ng-non-bindable template class="hidden">
		<form apply>
			<div class="form-group">
				<textarea title="请输入原因" ng-model="result.qjly" name="reason" required="required" class="form-control" style="resize: none;" rows="3"
					cols=""></textarea>
			</div>
			<div class="form-group">
				<div>{{result.fjStatus!=0?(result.fjStatus==1?'上传中':'上传完成'):'请上传附件,附件必须是图片格式'}}</div>
				<div upload class="btn btn-success col-md-6">选择附件</div>
			</div>
		</form>
	</div>
</div>
